package com.gomore.opple.module.account;

import android.text.TextUtils;

import com.gomore.opple.R;
import com.gomore.opple.data.DataRepository;
import com.gomore.opple.data.remote.retrofit.HttpResponseFunc;
import com.gomore.opple.data.remote.retrofit.RxSubscriber;
import com.gomore.opple.data.remote.retrofit.ServerResponseFunc;
import com.gomore.opple.exception.ApiException;
import com.gomore.opple.model.ShippingAddress;
import com.gomore.opple.model.ShippingAddressEntity;
import com.gomore.opple.module.IntentStart;
import com.gomore.opple.rest.employee.LoginResult;
import com.gomore.opple.rest.order.OrderBill;
import com.gomore.opple.rest.shippingaddr.RsShippingAddrEntity;
import com.gomore.opple.web.cgform.consumer.entity.TOConsumerEntity;
import com.gomore.opple.web.cgform.shippingaddr.entity.TOShippingAddrEntity;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;
import rx.subscriptions.CompositeSubscription;

public class AccountPresenter implements AccountContract.Presenter {

    private DataRepository mDataRepositroy;
    private final AccountContract.View mView;
    private CompositeSubscription mSubscriptions;
    List<ShippingAddress> shippingAddresses = new ArrayList<>();

    @Inject
    AccountPresenter(DataRepository dataRepository, AccountContract.View view) {
        mDataRepositroy = dataRepository;
        mView = view;
        mSubscriptions = new CompositeSubscription();
        mView.setPresenter(this);
    }


    @Override
    public void subscribe() {

    }

    @Override
    public void unsubscribe() {
        mSubscriptions.clear();
    }

    /**
     * 初始化数据
     */
    @Override
    public void prepareInitData() {
        mView.ShowConsumer(mDataRepositroy.getConsumer());
    }

    @Override
    public void queryAddress() {
        if (mDataRepositroy.getConsumer() == null) {
            return;
        }
        unsubscribe();
        shippingAddresses.clear();
        mView.showProgressDialog();
        Subscription subscription = mDataRepositroy.getShippingAddress(mDataRepositroy.getConsumer().getId())
                .map(new ServerResponseFunc<ShippingAddressEntity>())
                .onErrorResumeNext(new HttpResponseFunc<ShippingAddressEntity>())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new RxSubscriber<ShippingAddressEntity>() {
                    @Override
                    public void onNext(ShippingAddressEntity shippingAddressEntity) {
                        super.onNext(shippingAddressEntity);
                        mView.hideProgressDialog();
                        shippingAddresses.addAll(shippingAddressEntity.getList());
                        mView.showAddress();
                    }

                    @Override
                    protected void onError(ApiException ex) {
                        mView.hideProgressDialog();
                    }
                });
        mSubscriptions.add(subscription);
    }

    @Override
    public List<ShippingAddress> getAddressData() {
        return shippingAddresses;
    }

    @Override
    public LoginResult getUser() {
        return mDataRepositroy.getUser();
    }

    @Override
    public void saveOrder(OrderBill orderBill) {
        unsubscribe();
        mView.showProgressDialog();
        Subscription subscription = mDataRepositroy.saveOrder(orderBill)
                .map(new ServerResponseFunc<String>())
                .onErrorResumeNext(new HttpResponseFunc<String>())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new RxSubscriber<String>() {
                    @Override
                    public void onNext(String orderId) {
                        super.onNext(orderId);
                        mView.hideProgressDialog();
                        mView.showMessage(R.string.create_order_success);
                        if (!TextUtils.isEmpty(orderId)) {
                            mView.goToOrderPay(orderId);
                        }
                        //发送通知到购物车，删除指定商品，并finish当前页面
                        mView.deleteShoppingCar();
                    }

                    @Override
                    protected void onError(ApiException ex) {
                        mView.hideProgressDialog();
                        mView.showMessage(ex.message);
                    }
                });
        mSubscriptions.add(subscription);
    }
}
